--draw the HUD
local priority = 5

hudFont = love.graphics.newFont('assets/Science Fair.otf', 30)
hudFontSmall = love.graphics.newFont('assets/Science Fair.otf', 20)
hudFontLarge = love.graphics.newFont('assets/Science Fair.otf', 60)
hudFontGO = love.graphics.newFont('assets/Science Fair.otf', 200)

EntitySystem.rendersystem("drawHUD", priority, function()
    if level.rectangle.x then else
      createHUD()
    end
    
    updateHUD()
    
    --Detach camera to make HUD draw with window coordinates
    camera:detach()
    love.graphics.scale(game.window.scale.x)
    for e in pairs(EntitySystem.query("hud")) do
      --Rectangles
      if e.rectangle then
        for i = 1, #e.rectangle do
          love.graphics.setColor(e.rectangle[i].color)
          love.graphics.setLineWidth(e.rectangle[i].thick)
          love.graphics.rectangle(e.rectangle[i].fill, e.rectangle[i].x, e.rectangle[i].y, e.rectangle[i].width, e.rectangle[i].height)
        end
      end
      --Text
      if e.text then
        for i = 1, #e.text do
          love.graphics.setFont(e.text[i].font)
          --For vertical text
          if e.text[i].vert then
            love.graphics.setColor(e.text[i].color)
            
          else
            love.graphics.setColor(e.text[i].color)
            love.graphics.print(e.text[i].text, e.text[i].x, e.text[i].y)
          end
        end
      end
      --Images
      if e.hudImage then
        for i = 1, #e.hudImage do
          love.graphics.setColor(e.hudImage[i].color)
          love.graphics.draw(e.hudImage[i].image, e.hudImage[i].x, e.hudImage[i].y)
        end
      end
    end
    
    --
    love.graphics.setColor(color.white)
    love.graphics.setFont(hudFont)
    local t = "Score: "..(tostring(gameScore):reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", ""))
    love.graphics.print(t, 1920 - 20 - hudFont:getWidth(t), 20)
    t = "Sector: "..player.sector.x..", "..player.sector.y
    love.graphics.print(t, 20, 1080 - 20 - hudFont:getHeight(t))
    camera:attach()
end)
--
--
function updateHUD()
  --HEALTH
  --Pass player health to HUD
    local health = player.health.value
    level.text[2].text = health
    --Set health color based off of player health
    if health >= 75 then
      level.text[2].color = color.green    
    elseif health >= 50 then
      level.text[2].color = color.yellow
    elseif health >= 25 then
      level.text[2].color = color.orange
    else
      level.text[2].color = color.red
    end
    
  --RELOAD rectangle
  if player.reload.canFire then
    level.rectangle[1].height = -118
  else
    level.rectangle[1].height = math.clamp(-118,-118*player.reload.timer/player.reload.speed,0)
  end
  
  
  --Weapons Selection
  if player.weapons.selection == 1 then
    level.hudImage[1].color = color.green
    level.hudImage[2].color = color.white
  elseif player.weapons.selection == 2 then
     level.hudImage[1].color = color.white
     level.hudImage[2].color = color.green
  end
  
end
--
--
function createHUD()
  --Reload
  level.rectangle[1] = {x = 1920 - 45, y = 1080 - 21 - 100, width = 25, height = -118, fill = "fill", thick = 2, color = color.green}
  level.rectangle[2] = {x = 1920 - 45, y = 1080 - 20 - 100, width = 25, height = -120, fill = "line", thick = 2, color = color.white}
  --Weapons
  --[[level.rectangle[3] = {x = 1841, y = 1060, width = 60, height = -60, fill = "line", thick = 2, color = color.white}
  level.rectangle[4] = {x = 1766, y = 1060, width = 60, height = -60, fill = "line", thick = 2, color = color.white}
  level.rectangle[5] = {x = 1691, y = 1060, width = 60, height = -60, fill = "line", thick = 2, color = color.white}
  level.rectangle[6] = {x = 1766, y = 985, width = 60, height = -60, fill = "line", thick = 2, color = color.white}]]
  
  level.hudImage[1] = {x = 1841, y = 1080 - 20 - 62, image = love.graphics.newImage('assets/textures/HUD/lasersHUD.png'), color = color.white}
  --level.hudImage[2] = {x = 1841 - 15 - 62, y = 1080 - 20 - 62, image = love.graphics.newImage('assets/textures/HUD/lasersHUD.png'), color = color.white}
  level.hudImage[2] = {x = 1841 - 15 - 62, y = 1080 - 20 - 62, image = love.graphics.newImage('assets/textures/HUD/empHUD.png'), color = color.white}
  
  --Text
    level.text[1] = {text = "Health: ", x = 20, y = 20, color = color.white, font = hudFont}
    level.text[2] = {text = 100, x = 20 + hudFont:getWidth("Health: "), y = 20, color = color.green, font = hudFont}
end

